/**
 * title: 待办事项
 */

import { Component } from 'react';
import { Spin, Button, message, Row, Col } from 'antd';
import { connect } from 'dva';
import { formatDate, banWindowBack } from '@/utils'
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import CompanyInformation from './components/companyInformation';

@connect(({ todoDetail, loading, user }) => ({
  companyInformation: todoDetail.companyInformation,
  companyChangeLog: todoDetail.companyChangeLog,
  userInformation: todoDetail.userInformation,
  reviewResult: todoDetail.reviewResult,
  info: user.info,
  loading: !!loading['models']['todoDetail'],
}))
class TodoDetail extends Component {
  componentDidMount() {
    window.addEventListener('popstate',banWindowBack)
    this.init();
  }

  componentWillUnmount(){
    window.removeEventListener('popstate',banWindowBack)
  }

  init = () => {
    const { dispatch, location: { query } } = this.props;
    const { companyId, code, serialNo, status } = query;

    if(code === 'BL005'){
      dispatch({
        type: 'todoDetail/queryIdentityId',
        payload: {
          identityId: serialNo,
        },
      })
      .then((data) => {
        if(parseInt(status, 0) === 1){
            dispatch({
              type: 'todoDetail/queryIdentityApplyReviewResult',
              payload: {
                identityId: serialNo,
              },
            }).then((data) => {
              //console.log(data)
            }).catch(errMsg => {
              message.error(`${errMsg}`);
            });
        }
      })
      .catch(errMsg => {
        message.error(`${errMsg}`);
      });
    }

    if(code !== 'BL005'){
        // 获取当前公司信息
        dispatch({
          type: 'todoDetail/queryCompanyById',
          payload: {
            id:companyId,
          },
        })
          .then(({ state, applyId }) => {
            // 注意这个state，上面的state是通过url获取的，这里的state是读取公司详情后获取的
            //变更审核、变更驳回、创建审核、创建驳回要审核结果
            if (state === 2 || state === 3 || state === 4 || state === 5) {
              dispatch({
                type: 'todoDetail/queryCompanyApplyReviewResult',
                payload: {
                  id: serialNo,
                },
              }).catch(errMsg => {
                message.error(`${errMsg}`);
              });
            }

            // 变更暂存状态、变更审核中、变更被驳回时显示变更记录
            if (state === 7 || state === 2 || state === 3) {
              let payload = {
                companyId: companyId,
                state,
              };
              if (state === 2 || state === 3) {
                payload.applyId = applyId;
              }
              dispatch({
                type: 'todoDetail/queryCompanyChangeLog',
                payload,
              }).catch(errMsg => {
                message.error(`${errMsg}`);
              });
            }
          })
          .catch(errMsg => {
            message.error(`${errMsg}`);
          });
    }

  };

  render() {
    const {
        loading,
        userInformation,
        companyInformation,
        companyChangeLog,
        reviewResult,
        dispatch,
        history,
        info,
        location: {
          query: {  applicant, code, createTime, createUser, companyId, serialNo, status },
        },
      } = this.props,
      { name } = companyInformation;
    return (
      <PageHeaderWrapper
        title="待办详情"
      >
        <div className="container">
          <Spin spinning={loading}>
            <h2> {applicant} </h2>
            <Row>
              <Col span={8}>
                <div style={{ marginBottom: '10px' }}>申请人：{ createUser && createUser }</div>
              </Col>
              <Col span={14}>
                <div style={{ marginBottom: '10px' }}>申请时间：{ createTime && formatDate(createTime,'YYYY-MM-DD HH:mm:ss') }</div>
              </Col>
            </Row>
            <CompanyInformation
              information={companyInformation}
              dispatch={dispatch}
              history={history}
              companyChangeLog={companyChangeLog}
              reviewResult={reviewResult}
              userInformation={userInformation}
              companyId={companyId}
              serialNo={serialNo}
              code={code}
              status={parseInt(status, 0)}
              userStatus={info && info.status}
            />
          </Spin>
        </div>
      </PageHeaderWrapper>
    );
  }
}

export default TodoDetail;
